package Day2025_03_22;

public class LeetCode712 {
    public int minimumDeleteSum(String s1, String s2) {
        int sum = 0;
        int m = s1.length(), n = s2.length();
        for (int i = 0; i < m; i++) sum += s1.charAt(i);
        for (int j = 0; j < n; j++) sum += s2.charAt(j);
        s1 = " " + s1;
        s2 = " " + s2;
        int[][] dp = new int[m + 1][n + 1];
        for (int i = 1; i <= m; i++) {
            for (int j = 1; j <= n; j++) {
                if (s1.charAt(i) == s2.charAt(j)) dp[i][j] = dp[i - 1][j - 1] + s1.charAt(i);
                else dp[i][j] = Math.max(dp[i][j - 1], dp[i - 1][j]);
            }
        }
        return sum - dp[m][n] * 2;
    }
}
